package com.apexedu.sync.node.impl;

import com.apexedu.biz.Const;
import com.apexedu.framework.dao.BaseDao;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.entity.TSysRole;
import com.apexedu.portal.util.DBMUtil;
import com.apexedu.sync.contant.Constants;
import com.apexedu.sync.engine.ISyncContext;
import com.apexedu.sync.entity.SyncAppModelMsg;
import com.apexedu.sync.exception.AuCSyncException;
import com.apexedu.sync.node.ISyncNode;
import com.apexedu.sys.SysConstant;
import com.apexedu.sys.util.BusinessLogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 同步角色
 * Created by ly on 2015/1/12.
 */
public class RoleNode extends BaseNode implements ISyncNode {
	@Override
	public void execute(ISyncContext context) throws AuCSyncException {
		BaseDao baseDao = context.getBaseDao();
		DBMUtil dbmUtil = context.getDbmUtil(); //多数据源工具类

		String msg;
		try {
			String sql;
			if (context.isLivebos()) { //livebos系统
				sql = "select t.id as bizcode,t.name as rolename,t.rolecode as memo,1 as state,? as belongtoapp from LBROLE t";
			} else { //apexssh系统 TODO 待扩展，第三方
				sql = "select roleid as bizcode,rolename,state,memo,1 as state,? as belongtoapp from t_sys_role";
			}
			List<TSysRole> listAdd = dbmUtil.queryAllBeanList(sql, TSysRole.class, context.getAppid());
			List<TSysRole> listOld = baseDao.findByHql(" from TSysRole where belongtoapp = ?", context.getAppid());

			List<TSysRole> listRemove = new ArrayList<TSysRole>(); //删除集合
			List<TSysRole> listUpdate = new ArrayList<TSysRole>(); //更新集合                             
			//校验已存在的与已删除的
			for (TSysRole old : listOld) {
				boolean isExists = false;
				for (int i = 0; i < listAdd.size(); i++) {
					TSysRole add = listAdd.get(i);
					if (add.getBizcode().equals(old.getBizcode())) {
						isExists = true;
						add.setRoleid(old.getRoleid());
						listUpdate.add(add); //已存在的添加到更新集合中
						listAdd.remove(i); //从新增集合中删除
						break;
					}
				}
				if (!isExists) {
					listRemove.add(old); //已删除的添加到删除集合中
				}
			}
			if (!listAdd.isEmpty()) { //保存新增集合
				for (TSysRole add : listAdd) {
					add.setRoleid(SeqFactory.getNewSequenceAlone());
					context.getMapRole().put(add.getBizcode(), add); //保存到集合中，用于角色资源关系及用户角色关系校验
				}
				baseDao.saveAll(listAdd);
			}
			if (!listUpdate.isEmpty()) { //更新信息
				for (TSysRole role : listUpdate) {
					baseDao.updateNotNull(role);
					context.getMapRole().put(role.getBizcode(), role);//保存到集合中，用于角色资源关系及用户角色关系校验
				}
			}
			if (!listRemove.isEmpty())//删除已弃用的角色
				baseDao.deleteAll(listRemove);

			msg = "插入：" + listAdd.size() + "条，删除：" + listRemove.size() + "条，更新：" + listUpdate.size() + "条";
			BusinessLogUtil.log(getUserInfo(context), SysConstant.CZDX_T_SYS_ROLE, Const.LOG_ACTION_UPDATE, "", msg);
			context.getMsg().getListModelMsg().add(new SyncAppModelMsg(Constants.NodeType.ROLE, msg));
		} catch (Exception e) {
			e.printStackTrace();
			msg = e.getMessage();
			log.error("RoleNode error : " + e);
			context.getMsg().getListModelMsg().add(new SyncAppModelMsg(Constants.NodeType.ROLE, msg));
			throw new AuCSyncException(AuCSyncException.UNKNOWN_SERVICE_ERROR, msg);
		}
	}
}